﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using OAProject.Domain.Interfaces;
using OAProject.Domain.Entities;
using Microsoft.AspNetCore.Authorization;


namespace OAProject.API.Controllers
{
    /// <summary>
    /// 组织管理API控制器
    /// </summary>
    [Route("api/[controller]/[Action]")]
    [ApiController]
    [Authorize]     //全局授权过滤器
    public class DepartAPIController : ControllerBase
    {
        //依赖注入
        private IDepartmentRepository _departmentRepository;
        public DepartAPIController(IDepartmentRepository  departmentRepository)
        {
            this._departmentRepository = departmentRepository;
        }

        /// <summary>
        /// /组织管理菜单显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetIndex()
        {
            List<Department> list = _departmentRepository.Show();

            return Ok(new
            {
                data = list,
                code = 0
            });
        }

        /// <summary>
        /// 组织管理删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Delete(int id)
        {
            var n = _departmentRepository.Delete(id);

            return Ok(new
            {
                data = n
            });
        }

        /// <summary>
        /// 组织管理的添加
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Add([FromForm] Department obj)   //[FromForm]是接受页面传过来的数据
        {
           
            int a = _departmentRepository.Insert(obj);
            return Ok(a);
        }

        /// <summary>
        /// 组织管理的反填
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Fan(int id)
        {
            var list = _departmentRepository.GetById(id);
            return Ok(new
            {
                data = list
            });
        }

        /// <summary>
        /// 组织管理的修改
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult Upt([FromForm] Department obj)
        {
          
            var list = _departmentRepository.Update(obj);
            return Ok(list);
        }

    }
}
